from django import forms
from django.core.validators import email_re

class MultiEmailField(forms.Field):
    def to_python(self, value):
        "Normalize data to a list of strings."
        
        # Return an empty list if no input was given
        if not value:
            return []
        return value.split(',')
    
    def validate(self, value):
        "Check if value consists only of valid emails."
        
        # Use the parent's handling of required fields, etc.
        super(MultiEmailField, self).validate(value)
        
        for email in value:
            if not email_re.match(email):
                raise forms.ValidationError('%s is not a valid e-mail address.' % email)
            
    def widget_attrs(self, widget):
        return {
            'style': 'direction:ltr;text-align:left;'
        }